import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import javax.management.timer.Timer;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import ece.edt.CASClient;
import ece.edt.HyperPlanning;
import ece.edt.HyperPlanningDB;
import ece.edt.HyperPlanning.User;
import ece.edt.exceptions.CASClientException;
import ece.edt.exceptions.HyperPlanningException;

public class Suggest extends HttpServlet {
	private static final Logger log = Logger.getLogger(Grab.class);
	private static final long serialVersionUID = 1L;
    
	private String login;
	private String password;
	private String webauth;
	private String service;
	
	private CASClient casClient;
	private Connection connection;
	private HyperPlanning hp;
	
	private boolean use_database;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String start = request.getParameter("start");
			StringBuilder s = new StringBuilder();
			List<User> users = hp.getUsers(start);
	
			s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><options>");
			if(users != null){
				for(User user : users){
					s.append("<option>" + user.getName() + "</option>");
				}
			}
			s.append("</options>");
	
			DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
			df.setTimeZone(TimeZone.getTimeZone("GMT"));
			response.setHeader("Expires", df.format(new Date((new Date()).getTime() + Timer.ONE_DAY)));
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter writer = response.getWriter();
			
			response.setContentType("text/xml; charset=UTF-8");
			writer.println(s.toString());
		}catch (Exception e) {
			log.error(request.getQueryString() + " -> " + e.getMessage());
		}
	}
	
	@Override
	public void destroy() {
		super.destroy();
		if(use_database){
			try {
				((HyperPlanningDB)hp).closeConnection();
			} catch (SQLException e) {
				log.error(e, e);
			}
		}
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		ServletContext conf = config.getServletContext();
		login = conf.getInitParameter("cas-login");
		password = conf.getInitParameter("cas-password");
		webauth = conf.getInitParameter("cas-webauth");
		service = conf.getInitParameter("cas-service");
		use_database = Boolean.parseBoolean(conf.getInitParameter("use_database"));
		
		if(use_database){
			try {
				Context ctx = new InitialContext();
				DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/edtgrabber");
	
				connection = ds.getConnection();
			} catch (NamingException e) {
				log.error(e, e);
			} catch (SQLException e) {
				log.error(e, e);
			}
		}
		
		try {
			casClient = CASClient.getInstance(webauth, service, login, password);
			if(use_database){
				hp = HyperPlanningDB.getInstance(casClient, connection);
			}else{
				hp = HyperPlanning.getInstance(casClient);
			}
		} catch (CASClientException e) {
			log.error(e);
		} catch (HyperPlanningException e) {
			log.error(e);
		}
	}
}
